package com.cyou.nad.qb.task;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;


import com.cyou.qb.common.domain.Code;
import com.cyou.qb.common.service.CodeService;
import com.cyou.qb.constants.DBStoreConstants;
import com.cyou.qb.face.entity.NameValue;
import com.cyou.qb.face.exception.QbGetDataException;
import com.cyou.qb.face.service.GetGameInfoService;
import com.cyou.qb.face.service.SyncGameTask;

/**
 * <p>游戏信息相关的定时任务</p>
 * 同步游戏名称<br>
 * 同步游戏类型
 * @author zoudongchao
 */
@Service("gameInfoManage")
public class GameInfoManage extends SyncGameTask{
	
	@Resource(name="QbGameImpl")
	private GetGameInfoService getGameInfoService;
	
	@Autowired
	private CodeService codeService;
	
	private static Logger logger = Logger.getLogger(GameInfoManage.class);
	
	/**
	 * <p>同步游戏名称及gamecode</p>
	 * @return 成功与否
	 */
	public boolean syncGameName() {
		logger.info("[GameInfoManage]start to sync data...");
		List<NameValue> gameNameValueList = new ArrayList<NameValue>();
		//调用接口 获取数据
		try {
			gameNameValueList = getGameInfoService.getGameNameInfo();
		} catch (QbGetDataException e) {
			logger.error("[GameInfoManage]failed to get game name info from 17173",e);
			return false;
		}
		//没有游戏信息
		if (gameNameValueList.size() == 0)
			return true;
		//删除本地库游戏名称信息
		codeService.deleteDataByType(DBStoreConstants.TYPE_GAMENAME);
		//数据写入
		List<Code> codeList = new ArrayList<Code>();
		for(NameValue nameValue : gameNameValueList) {
			Code code = new Code();
			code.setName(nameValue.getStrName());
			code.setValue(nameValue.getStrValue());
			code.setParentName("");
			code.setType(DBStoreConstants.TYPE_GAMENAME);
			code.setMemo("游戏名称及GameCode");
			codeList.add(code);
		}
		codeService.insertIntoCode(codeList);
		
		logger.info("[GameInfoManage]end sync data...");
		return true;
	}
	
	/**
	 * <p>同步游戏类型数据</p>
	 * @author zoudongchao
	 * @return 成功与否
	 */
	public boolean syncGameType(){
		logger.info("[GameInfoManage]start to sync data...");
		List<NameValue> gameTypeList = new ArrayList<NameValue>();
		try {
			gameTypeList = getGameInfoService.getGameTypeInfo();
		} catch (QbGetDataException e) {
			logger.error("[GameInfoManage]failed to get game type from 17173",e);
			return false;
		}
		if (gameTypeList.size() == 0) {
			return true;
		}
		//删除本地游戏类型数据
		codeService.deleteDataByType(DBStoreConstants.TYPE_GAMETYPE);
		//数据写入
		List<Code> codeList = new ArrayList<Code>();
		for(NameValue nameValue : gameTypeList) {
			Code code = new Code();
			code.setName(nameValue.getStrName());
			code.setValue(nameValue.getStrValue());
			code.setParentName("");
			code.setType(DBStoreConstants.TYPE_GAMETYPE);
			code.setMemo("游戏类型");
			codeList.add(code);
		}
		codeService.insertIntoCode(codeList);
		logger.info("[GameInfoManage]end sync data...");
		return true;
	}

	/***********************************getter & setter*************************************/
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		SyncGameTask.BEAN_FACTORY=beanFactory;
	}
	

	public GetGameInfoService getGetGameInfoService() {
		return getGameInfoService;
	}

	public void setGetGameInfoService(GetGameInfoService getGameInfoService) {
		this.getGameInfoService = getGameInfoService;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	
}
